package com.happy.ibidding.service;

import com.happy.ibidding.bean.Bid;
import com.happy.ibidding.bean.Comment;
import com.happy.ibidding.dao.CommentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class CommentService {

    final private CommentRepository commentRepository;

    @Autowired
    public CommentService(CommentRepository commentRepository) {
        this.commentRepository = commentRepository;
    }


    public Comment save(Comment comment) {
        return this.commentRepository.save(comment);
    }

    public List<Comment> viewAll() {
        return this.commentRepository.findAll();
    }

    public List<Comment> viewAll(String askId, String userId) {
        List<Comment> commentList = this.viewAll();

        if (Objects.nonNull(askId)) {
            commentList = commentList.parallelStream().filter(each -> each.getAskId().equals(askId)).collect(Collectors.toList());
        }

        if (Objects.nonNull(userId)) {
            commentList = commentList.parallelStream().filter(each -> each.getUserId().equals(userId)).collect(Collectors.toList());
        }

        return commentList;
    }

    public void delete(String id){
        this.commentRepository.deleteById(id);
    }

}
